package controller;

import dao.UserDAO;
import model.User;
import java.util.List;

/**
 * 用户控制器
 */
public class UserController {
    private UserDAO userDAO;

    public UserController() {
        this.userDAO = new UserDAO();
    }

    /**
     * 用户登录
     */
    public User login(String studentId, String password) {
        try {
            return userDAO.login(studentId, password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("登录失败: " + e.getMessage());
        }
    }

    /**
     * 用户注册
     */
    public boolean register(User user) {
        try {
            // 检查学号是否已存在
            if (userDAO.existsByStudentId(user.getStudentId())) {
                return false;
            }
            return userDAO.insertUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("注册失败: " + e.getMessage());
        }
    }

    /**
     * 更新用户信息
     */
    public boolean updateUser(User user) {
        try {
            return userDAO.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新用户信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        try {
            return userDAO.getAllUsers();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取用户列表失败: " + e.getMessage());
        }
    }

    /**
     * 搜索用户
     */
    public List<User> searchUsers(String keyword) {
        try {
            return userDAO.searchUsers(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("搜索用户失败: " + e.getMessage());
        }
    }

    /**
     * 重置密码
     */
    public boolean resetPassword(int userId, String newPassword) {
        try {
            return userDAO.resetPassword(userId, newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("重置密码失败: " + e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    public boolean deleteUser(int userId) {
        try {
            return userDAO.deleteUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除用户失败: " + e.getMessage());
        }
    }

    /**
     * 更新用户信誉分
     */
    public boolean updateUserCreditScore(int userId, int creditScore) {
        try {
            return userDAO.updateUserCreditScore(userId, creditScore);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新信誉分失败: " + e.getMessage());
        }
    }

    /**
     * 禁用用户
     */
    public boolean banUser(int userId, java.time.LocalDate banUntilDate) {
        try {
            return userDAO.banUser(userId, banUntilDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("禁用用户失败: " + e.getMessage());
        }
    }

    /**
     * 解禁用户
     */
    public boolean unbanUser(int userId) {
        try {
            return userDAO.unbanUser(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解禁用户失败: " + e.getMessage());
        }
    }

    /**
     * 获取信誉分排行榜
     */
    public List<User> getCreditScoreRanking(int limit) {
        try {
            return userDAO.getCreditScoreRanking(limit);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取信誉分排行榜失败: " + e.getMessage());
        }
    }

    /**
     * 获取被禁用的用户列表
     */
    public List<User> getBannedUsers() {
        try {
            return userDAO.getBannedUsers();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取禁用用户列表失败: " + e.getMessage());
        }
    }
}
